package jast.util.sync;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import jast.util.es.ElasticSearchClientManager;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.WriteRequest;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.reindex.*;
import org.elasticsearch.script.Script;
import org.elasticsearch.script.ScriptType;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.collapse.CollapseBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;


/**
 * @author Jast
 * @description ElasticSearch操作类
 * @date 2020-12-11 13:59
 */
@Slf4j(topic = "common")
public class ElasticSearchClientServiceImpl {


    /**
     * 设置分页参数
     *
     * @param pageSize
     * @param pageNum
     * @param searchRequestBuilder
     * @return void
     * @name setPageParam
     * @date 2021/4/9 2:37 下午
     * @author Jast
     */
    private void setPageParam(Integer pageSize, Integer pageNum, SearchRequestBuilder searchRequestBuilder) {
        int from = 0;
        int size = 10;
        if (pageNum != null && pageSize != null) {
            if (pageNum >= 1) {
                from = (pageNum - 1) * pageSize;
            }
            size = pageSize > 0 ? pageSize : 10;
        }
        searchRequestBuilder.setFrom(from) //设置从第几条数据开始查询-分页
                .setSize(size); //设置返回的数据量
    }

    public String query(String index,Integer pageSize,Integer pageNum) throws Exception {
        SearchRequestBuilder searchRequestBuilder = ElasticSearchClientManager.getESClient()
                .prepareSearch(index);
//                .setTypes(esQuery.getType());
        //设置返回total为全部-7.x版本默认返回total为10000
        searchRequestBuilder.setTrackTotalHits(true);
        //----------------------------------------------
        //设置分页参数
        this.setPageParam(pageSize,pageNum, searchRequestBuilder);
        //----------------------------------------------

        log.info("查询条件:\n{}",JSONUtil.toJsonPrettyStr(JSONUtil.parseObj(searchRequestBuilder.toString())));
        //执行获取结果
        SearchResponse response = searchRequestBuilder.get();
        log.info("查询结果:{}",response.toString());
        log.info("------------------------------------------------------------------------");

        return response.toString();
    }
}
